#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QtGui/QTextEdit>
#include <QtGui/QLineEdit>
#include <QtGui/QSpinBox>
#include <QtGui/QProgressDialog>
#include <QtGui/QPushButton>
#include <QtGui/QCheckBox>
#include <QtGui/QLabel>
#include <QtCore>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QtGlobal>
#include <QFile>
#include <QFileInfo>
#include <QList>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QStringList>
#include <QUrl>
#include <QObject>

class MainWindow : public QMainWindow
{
	Q_OBJECT
	QLineEdit *urlLineEdit, *pathLineEdit;
	QSpinBox *v_min;
	QSpinBox *v_max;
	QUrl url;
	QProgressBar *progressBar, *downloadBar;
	QPushButton *downloadButton, *stopButton;
	QCheckBox *indexBox;
	QLabel *downloadLabel;
	QList<QNetworkReply *> currentDownloads;
	QString savePath, journalUrl, volumeMin, volumeMax, current_volume, nextlink;
	QNetworkReply *replySD;
	int current_issue, current_article;
	long int totalErrors;
	qint64 totalSize;
	bool is_stopped;
	QList<QByteArray> issueList;
	QByteArray page;
	QStringList volumeList, listOfIssueUrls, listOfArticleUrls, listOfPaths, listOfArticlePages;
	QTimer time_out;
public slots:
	void getJournalInfo();
	void stop() {is_stopped = true;}
	void processVolumes(QNetworkReply *reply);
	void processIssuesSL(QNetworkReply *reply);
	void processIssuesSD(QNetworkReply *reply);
	void processVolumesIW(QNetworkReply *reply);
	void abortSD();
	void Download(QNetworkReply *reply);
	void scrollLog();
	void urlChanged(QString string);
	void selectUrl();
	void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
	void help();
public:
	QString site;
	MainWindow(QWidget *parent = 0);
	QNetworkAccessManager qnam_volumes, qnam_volumesIW, qnam_issuesSL, qnam_issuesSD, qnam_articles;
	QNetworkCookieJar *cookiesJar;
	QTextEdit *logTextEdit;
	void parse();
	void readSettings();
	void writeSettings();
	QString saveFileName(const QUrl &url);
	QString saveToDisk(const QString &path, const QString &filename, QIODevice *data);
	~MainWindow();
private:
	void resetUI();
protected:
   void closeEvent(QCloseEvent *event);
};

#endif // MAINWINDOW_H
